<template>
  <div class="kawaii-space-home">
    <!-- 公告区域 -->
    <div v-if="announcement" class="kawaii-announcement-card">
      <div class="kawaii-card-header">
        <div class="kawaii-card-title">
          <span class="kawaii-emoji">📢</span>
          公告板
        </div>
        <div
          v-if="isOwner"
          class="kawaii-edit-btn"
          @click="$emit('showAnnouncementDialog')"
        >
          <span class="kawaii-edit-icon">✏️</span>
        </div>
      </div>
      <div class="kawaii-announcement-content">{{ announcement }}</div>
    </div>
    <div
      v-else-if="isOwner"
      class="kawaii-empty-announcement"
      @click="$emit('showAnnouncementDialog')"
    >
      <div class="kawaii-empty-icon">📝</div>
      <div class="kawaii-empty-text">添加公告，让粉丝了解你的动态~</div>
    </div>

    <!-- 个人资料卡片 -->
    <div class="kawaii-profile-card">
      <div class="kawaii-card-header">
        <div class="kawaii-card-title">
          <span class="kawaii-emoji">👤</span>
          个人资料
        </div>
        <div
          v-if="isOwner"
          class="kawaii-edit-btn"
          @click="$emit('showEditDialog')"
        >
          <span class="kawaii-edit-icon">✏️</span>
        </div>
      </div>

      <div class="kawaii-profile-content">
        <div class="kawaii-profile-item" v-if="userInfo.nickname">
          <div class="kawaii-item-label">昵称</div>
          <div class="kawaii-item-value">{{ userInfo.nickname }}</div>
        </div>

        <div class="kawaii-profile-item" v-if="userInfo.gender !== undefined">
          <div class="kawaii-item-label">性别</div>
          <div class="kawaii-item-value">
            <span v-if="userInfo.gender === 1">男生 👦</span>
            <span v-else-if="userInfo.gender === 2">女生 👧</span>
            <span v-else>保密 🤐</span>
          </div>
        </div>

        <div class="kawaii-profile-item" v-if="userInfo.birthday">
          <div class="kawaii-item-label">生日</div>
          <div class="kawaii-item-value">
            {{ formatDate(userInfo.birthday) }}
          </div>
        </div>

        <div class="kawaii-profile-item" v-if="userInfo.signature">
          <div class="kawaii-item-label">个性签名</div>
          <div class="kawaii-item-value signature">
            {{ userInfo.signature }}
          </div>
        </div>
      </div>
    </div>

    <!-- 最近投稿卡片 -->
    <div class="kawaii-recent-videos-card">
      <div class="kawaii-card-header">
        <div class="kawaii-card-title">
          <span class="kawaii-emoji">🎬</span>
          最近投稿
        </div>
        <div class="kawaii-view-more" @click="switchToVideoTab">
          查看更多
          <span class="kawaii-arrow">→</span>
        </div>
      </div>

      <div v-if="recentVideos.length > 0" class="kawaii-videos-grid">
        <div
          v-for="video in recentVideos"
          :key="video.id"
          class="kawaii-video-item"
          @click="goToVideo(video.id)"
        >
          <div class="kawaii-video-cover">
            <img :src="video.cover" alt="视频封面" />
            <div class="kawaii-video-duration">
              {{ formatDuration(video.duration) }}
            </div>
          </div>
          <div class="kawaii-video-info">
            <div class="kawaii-video-title">{{ video.title }}</div>
            <div class="kawaii-video-stats">
              <span class="kawaii-stat">
                <span class="kawaii-stat-icon">👁️</span>
                {{ formatCount(video.playCount) }}
              </span>
              <span class="kawaii-stat">
                <span class="kawaii-stat-icon">💬</span>
                {{ formatCount(video.commentCount) }}
              </span>
              <span class="kawaii-video-date">
                {{ formatTime(video.createTime) }}
              </span>
            </div>
          </div>
        </div>
      </div>

      <div v-else class="kawaii-empty-videos">
        <div class="kawaii-empty-icon">🎬</div>
        <div class="kawaii-empty-text">暂无投稿视频</div>
        <el-button
          v-if="isOwner"
          type="primary"
          class="kawaii-upload-btn"
          @click="goToUpload"
        >
          立即投稿
        </el-button>
      </div>
    </div>

    <!-- 最近收藏卡片 -->
    <div class="kawaii-recent-favorites-card">
      <div class="kawaii-card-header">
        <div class="kawaii-card-title">
          <span class="kawaii-emoji">⭐</span>
          最近收藏
        </div>
        <div class="kawaii-view-more" @click="switchToFavoriteTab">
          查看更多
          <span class="kawaii-arrow">→</span>
        </div>
      </div>

      <div v-if="recentFavorites.length > 0" class="kawaii-videos-grid">
        <div
          v-for="video in recentFavorites"
          :key="video.id"
          class="kawaii-video-item"
          @click="goToVideo(video.id)"
        >
          <div class="kawaii-video-cover">
            <img :src="video.cover" alt="视频封面" />
            <div class="kawaii-video-duration">
              {{ formatDuration(video.duration) }}
            </div>
          </div>
          <div class="kawaii-video-info">
            <div class="kawaii-video-title">{{ video.title }}</div>
            <div class="kawaii-video-stats">
              <span class="kawaii-stat">
                <span class="kawaii-stat-icon">👁️</span>
                {{ formatCount(video.playCount) }}
              </span>
              <span class="kawaii-stat">
                <span class="kawaii-stat-icon">💬</span>
                {{ formatCount(video.commentCount) }}
              </span>
              <span class="kawaii-video-date">
                {{ formatTime(video.createTime) }}
              </span>
            </div>
          </div>
        </div>
      </div>

      <div v-else class="kawaii-empty-favorites">
        <div class="kawaii-empty-icon">⭐</div>
        <div class="kawaii-empty-text">暂无收藏视频</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineProps, defineEmits } from 'vue'
import { useRouter } from 'vue-router'

const props = defineProps({
  userId: {
    type: String,
    required: true,
  },
  isOwner: {
    type: Boolean,
    default: false,
  },
})

const emit = defineEmits([
  'showEditDialog',
  'showAnnouncementDialog',
  'switchTab',
])

const router = useRouter()

// 用户数据
const userInfo = ref({
  nickname: '萌萌用户',
  gender: 0,
  birthday: '',
  signature: '这个人很懒，什么也没留下~',
})

// 公告内容
const announcement = ref('')

// 最近投稿视频
const recentVideos = ref([
  {
    id: 1,
    title: '【初音未来】甜甜的歌曲翻唱合集',
    cover: '/src/assets/anime/images/banner1.png',
    duration: 245,
    playCount: 2345,
    commentCount: 123,
    createTime: '2023-05-15T08:30:00',
  },
  {
    id: 2,
    title: '【萌系日常】猫咪的一天超治愈',
    cover: '/src/assets/anime/images/banner2.png',
    duration: 362,
    playCount: 5678,
    commentCount: 234,
    createTime: '2023-05-10T10:15:00',
  },
  {
    id: 3,
    title: '【游戏实况】最新动漫游戏试玩',
    cover: '/src/assets/anime/images/banner3.png',
    duration: 1234,
    playCount: 1234,
    commentCount: 56,
    createTime: '2023-05-05T15:45:00',
  },
])

// 最近收藏视频
const recentFavorites = ref([
  {
    id: 4,
    title: '【动画电影】新海诚作品赏析',
    cover: '/src/assets/anime/images/banner4.png',
    duration: 986,
    playCount: 9876,
    commentCount: 543,
    createTime: '2023-04-20T14:20:00',
  },
  {
    id: 5,
    title: '【美食】日式甜点制作教程',
    cover: '/src/assets/anime/images/banner5.png',
    duration: 782,
    playCount: 3456,
    commentCount: 198,
    createTime: '2023-04-15T16:30:00',
  },
])

// 获取用户信息
const fetchUserInfo = async () => {
  // 这里应该是API调用
  // 模拟数据
  setTimeout(() => {
    userInfo.value = {
      nickname: '萌系少女',
      gender: 2,
      birthday: '2000-01-15',
      signature: '喜欢动漫、游戏和可爱的事物 (๑•̀ㅂ•́)و✧',
    }
    announcement.value =
      '欢迎来到我的个人空间！最近在更新萌系视频，喜欢的话请关注我吧~ ヾ(≧▽≦*)o'
  }, 500)
}

// 格式化数字
const formatCount = (count) => {
  if (count >= 10000) {
    return (count / 10000).toFixed(1) + '万'
  }
  return count.toString()
}

// 格式化视频时长
const formatDuration = (seconds) => {
  const minutes = Math.floor(seconds / 60)
  const remainingSeconds = seconds % 60
  return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`
}

// 格式化时间
const formatTime = (timeString) => {
  const date = new Date(timeString)
  const now = new Date()
  const diffDays = Math.floor((now - date) / (1000 * 60 * 60 * 24))

  if (diffDays === 0) {
    return '今天'
  } else if (diffDays === 1) {
    return '昨天'
  } else if (diffDays < 7) {
    return `${diffDays}天前`
  } else if (diffDays < 30) {
    return `${Math.floor(diffDays / 7)}周前`
  } else if (diffDays < 365) {
    return `${Math.floor(diffDays / 30)}个月前`
  } else {
    return `${Math.floor(diffDays / 365)}年前`
  }
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return '未设置'

  const date = new Date(dateString)
  const year = date.getFullYear()
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')

  return `${year}-${month}-${day}`
}

// 切换到视频标签页
const switchToVideoTab = () => {
  emit('switchTab', 'videos')
}

// 切换到收藏标签页
const switchToFavoriteTab = () => {
  emit('switchTab', 'favorites')
}

// 去视频详情页
const goToVideo = (videoId) => {
  router.push(`/front/video/${videoId}`)
}

// 去上传页
const goToUpload = () => {
  router.push('/upload')
}

onMounted(() => {
  fetchUserInfo()
})
</script>

<style scoped lang="scss">
.kawaii-space-home {
  padding: 20px 0;
}

/* 卡片通用样式 */
.kawaii-announcement-card,
.kawaii-profile-card,
.kawaii-recent-videos-card,
.kawaii-recent-favorites-card,
.kawaii-empty-announcement {
  background: rgba(255, 255, 255, 0.8);
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 24px;
  box-shadow: 0 4px 12px rgba(255, 182, 193, 0.15);
  border: 2px solid rgba(255, 182, 193, 0.3);
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(to right, #ffcbe3, #c9a0dc);
    border-radius: 4px 4px 0 0;
  }
}

/* 卡片标题栏 */
.kawaii-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;

  .kawaii-card-title {
    font-size: 18px;
    font-weight: bold;
    color: #7c5479;
    display: flex;
    align-items: center;

    .kawaii-emoji {
      margin-right: 8px;
      font-size: 20px;
    }
  }

  .kawaii-edit-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: rgba(255, 203, 227, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s;

    &:hover {
      background: rgba(255, 203, 227, 0.7);
      transform: rotate(15deg);
    }
  }

  .kawaii-view-more {
    font-size: 14px;
    color: #fe4ea4;
    cursor: pointer;
    display: flex;
    align-items: center;
    transition: all 0.3s;

    .kawaii-arrow {
      margin-left: 3px;
      transition: transform 0.3s;
    }

    &:hover {
      .kawaii-arrow {
        transform: translateX(3px);
      }
    }
  }
}

/* 公告区域 */
.kawaii-announcement-content {
  color: #7c5479;
  line-height: 1.6;
  font-size: 15px;
  background: rgba(255, 203, 227, 0.1);
  padding: 15px;
  border-radius: 12px;
  position: relative;

  &::before {
    content: '❝';
    position: absolute;
    top: 5px;
    left: 8px;
    font-size: 20px;
    color: rgba(254, 78, 164, 0.2);
  }

  &::after {
    content: '❞';
    position: absolute;
    bottom: 5px;
    right: 8px;
    font-size: 20px;
    color: rgba(254, 78, 164, 0.2);
  }
}

.kawaii-empty-announcement {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 30px 20px;
  cursor: pointer;
  transition: all 0.3s;

  &:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 15px rgba(255, 182, 193, 0.25);
  }

  .kawaii-empty-icon {
    font-size: 32px;
    margin-bottom: 12px;
  }

  .kawaii-empty-text {
    color: #99809d;
    font-size: 15px;
  }
}

/* 个人资料 */
.kawaii-profile-content {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.kawaii-profile-item {
  .kawaii-item-label {
    font-size: 14px;
    color: #99809d;
    margin-bottom: 5px;
  }

  .kawaii-item-value {
    font-size: 16px;
    color: #7c5479;
    word-break: break-word;

    &.signature {
      grid-column: span 2;
      font-size: 15px;
      line-height: 1.5;
      background: rgba(255, 203, 227, 0.1);
      padding: 10px;
      border-radius: 8px;
    }
  }

  &:last-child {
    grid-column: span 2;
  }
}

/* 视频网格 */
.kawaii-videos-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
}

.kawaii-video-item {
  cursor: pointer;
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.3s;
  box-shadow: 0 3px 8px rgba(255, 182, 193, 0.15);

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 15px rgba(255, 182, 193, 0.25);

    .kawaii-video-title {
      color: #fe4ea4;
    }
  }

  .kawaii-video-cover {
    position: relative;
    width: 100%;
    padding-top: 60%;
    overflow: hidden;

    img {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .kawaii-video-duration {
      position: absolute;
      bottom: 5px;
      right: 5px;
      background: rgba(0, 0, 0, 0.7);
      color: white;
      padding: 2px 5px;
      border-radius: 4px;
      font-size: 12px;
    }
  }

  .kawaii-video-info {
    padding: 10px;
    background: white;
  }

  .kawaii-video-title {
    font-size: 14px;
    color: #7c5479;
    margin-bottom: 8px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    height: 42px;
    line-height: 1.5;
    transition: color 0.3s;
  }

  .kawaii-video-stats {
    display: flex;
    font-size: 12px;
    color: #99809d;
  }

  .kawaii-stat {
    display: flex;
    align-items: center;
    margin-right: 10px;

    .kawaii-stat-icon {
      margin-right: 3px;
      font-size: 14px;
    }
  }

  .kawaii-video-date {
    margin-left: auto;
  }
}

/* 空状态 */
.kawaii-empty-videos,
.kawaii-empty-favorites {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 0;

  .kawaii-empty-icon {
    font-size: 36px;
    margin-bottom: 15px;
  }

  .kawaii-empty-text {
    color: #99809d;
    font-size: 15px;
    margin-bottom: 15px;
  }
}

.kawaii-upload-btn {
  background: linear-gradient(to right, #fe4ea4, #ff9fcf);
  color: white;
  border: none;
  margin-top: 10px;

  &:hover {
    background: linear-gradient(to right, #f33b95, #ff8cc3);
    transform: translateY(-2px);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .kawaii-profile-content {
    grid-template-columns: 1fr;
  }

  .kawaii-profile-item {
    &:last-child {
      grid-column: span 1;
    }
  }

  .kawaii-videos-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  }
}
</style>
